/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { seoMap } from '@base/config';
import { isEqual } from 'lodash';
import React from 'react';
import { Helmet, isBrowser, Location, useLocation } from 'umi';

export interface SeoProps {
  title?: string;
  keywords?: string;
  description?: string;
}

const Seo: React.FC<SeoProps> = (props) => {
  const { title: propsTitle, keywords: propsKeywords, description: propsDescription } = props;

  const location: Location = useLocation();

  let { keywords, description, title } = {
    ...seoMap.other,
    ...seoMap[location.pathname],
  };
  title = title || propsTitle;
  keywords = propsKeywords || keywords;
  description = propsDescription || description;

  if (isBrowser()) {
    // 修正：点击菜单异常修改title
    // Link引起标题变更，无法触发刷新
    setTimeout(() => {
      if (title && document.title !== title) {
        document.title = title;
      }
    }, 10);
  }

  return (
    <>
      <Helmet>
        {title && <title>{title}</title>}
        {keywords && <meta name='keywords' content={keywords} />}
        {description && <meta name='description' content={description} />}
      </Helmet>
    </>
  );
};

export default React.memo(Seo, isEqual);
